package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.RoleDao;
import com.aaa.book_mall.entity.Role;
import com.aaa.book_mall.util.LayTab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description ToDo
 * @Auther teacher Bai
 * @Date 2020/12/28 9:06
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleDao roleDao;

    /**
     * 角色页面展示所有的角色信息
     * @return
     */
    @RequestMapping("/getAllRole")
    public LayTab getAllRole(){
        List<Map<String, Object>> roles = roleDao.getAllRole();
        LayTab layTab = new LayTab(roles.size(), roles);
        System.out.println(layTab);
        return layTab;
    }


    /**
     * 新增和修改角色的表单提交方法
     * @param role
     * @return
     */
    @RequestMapping("/editRole")
    public Map editRole(Role role){

        Map<Object, Object> map = new HashMap<>();
        //判断，如果传来的数据有rid，证明是修改，否则，就是新增
        int i = 0;
        if(role.getRid()==null || "".equals(role.getRid())){
            i = roleDao.insertRole(role);
            if(i > 0){
                map.put("code","200");
                map.put("msg","新增成功");
            }
        }else{
            i = roleDao.updateRole(role);
            if(i > 0){
                map.put("code","200");
                map.put("msg","修改成功");
            }
        }
        if(i == -1){
            map.put("code","300");
            map.put("msg","操作失败");
        }
        return map;
    }

}
